<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('3d打印')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
                <a-menu-item v-if="record.bpmStatus === '1'">
                <a @click="startProcess(record)">发起流程</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item >
                <a @click="handlePreviewPic(record)">审批进度</a>
                <a  @click="handleProcess(record)">办理</a>
               </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <table-dayin-modal ref="modalForm" @ok="modalFormOk"/>
    <ext-act-process-inst-pic-modal ref="extActProcessInstPicModal"></ext-act-process-inst-pic-modal>
    <bpm-biz-task-deal-modal :path="path" :formData="formData" ref="taskDealModal" @ok="taskOk" />

    <task-deal-modal ref="taskDealModal" :path="path" :formData="formData" @ok="handleOk"></task-deal-modal>
    <select-entruster-modal ref="selectEntrusterModal" @selectFinished="handleEntruster"></select-entruster-modal>
    <task-notify-me-modal ref="taskNotifyMeModal"></task-notify-me-modal>
    <select-single-user-modal ref="selectSingleUserModal" @selectFinished="selectUserOK"></select-single-user-modal>
  </a-card>
</template>

<script>
  import ExtActProcessInstPicModal from '@views/modules/extbpm/process/modules/ExtActProcessInstPicModal.vue'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import TableDayinModal from './TableDayinModal.vue'
  import { filterDictText, filterMultiDictText, initDictOptions } from '@comp/dict/JDictSelectUtil'
  import '@assets/less/TableExpand.less'
  import { getAction, postAction, putAction } from '@api/manage'
  import BpmBizTaskDealModal from '@views/modules/bpmbiz/common/BpmBizTaskDealModal.vue'
  import TaskDealModal from '@views/modules/bpm/task/TaskDealModal.vue'
  import SelectEntrusterModal from '@views/modules/bpm/task/form/SelectEntrusterModal.vue'
  import TaskNotifyMeModal from "@views/modules/extbpm/process/TaskNotifyMeModal.vue"
  import SelectSingleUserModal from "@views/modules/bpm/task/form/SelectSingleUserModal.vue"

  export default {
    name: "TableDayinList",
    mixins:[JeecgListMixin],
    components: {
      SelectEntrusterModal, SelectSingleUserModal, TaskDealModal, TaskNotifyMeModal,
      BpmBizTaskDealModal,
      TableDayinModal,
      ExtActProcessInstPicModal,
    },
    data () {
      return {
        description: '3d打印管理页面',
        bpmStatusDictOptions:[],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'流程状态',
            align:"center",
            dataIndex: 'bpmStatus',
            customRender: (text) => {
              //字典值替换通用方法
              console.log(this.bpmStatusDictOptions)
              return filterDictText(this.bpmStatusDictOptions, text);
            }
          },
         // {
         //    title:'任务编号',
         //    align:"center",
         //    dataIndex: 'taskId',
         //    hidden:true
         //  },
          {
            title:'任务名称',
            align:"center",
            dataIndex: 'taskName'
          },
          {
            title:'任务内容',
            align:"center",
            dataIndex: 'taskNeirong'
          },
          {
            title:'订单编号',
            align:"center",
            dataIndex: 'dingdanBianhao'
          },
          {
            title:'客户名称',
            align:"center",
            dataIndex: 'dingdanKehu'
          },
          {
            title:'模型件数',
            align:"center",
            dataIndex: 'dingdanShuliang'
          },
          {
            title:'模型重克',
            align:"center",
            dataIndex: 'dingdanMoxingzhongliang'
          },
          {
            title:'下单时间',
            align:"center",
            dataIndex: 'dingdanXiadanshijian'
          },
          {
            title:'接单员',
            align:"center",
            dataIndex: 'dingdanJiedanyuan'
          },
          {
            title:'订单要求',
            align:"center",
            dataIndex: 'dingdanYaoqiu'
          },
          {
            title:'模型图片',
            align:"center",
            dataIndex: 'dingdanTupian'
          },
          {
            title:'货款状态',
            align:"center",
            dataIndex: 'dingdanHuokuan'
          },
          {
            title:'发货方式',
            align:"center",
            dataIndex: 'dingdanFahuofangshi'
          },
          {
            title:'订单状态',
            align:"center",
            dataIndex: 'dingdanZhuangtai'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/dayin/tableDayin/list",
          delete: "/dayin/tableDayin/delete",
          deleteBatch: "/dayin/tableDayin/deleteBatch",
          exportXlsUrl: "/dayin/tableDayin/exportXls",
          importExcelUrl: "dayin/tableDayin/importExcel",
          startProcess: '/act/process/extActProcess/startMutilProcess' ,
          claim: "/act/task/claim",
          taskEntrust:"/act/task/taskEntrust",
        },
        flowCode: 'onl_table_dayin',
        dictOptions:{},
        superFieldList:[],
        path:"",
        formData:{},
        taskId:'',
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
        initDictOptions('bpm_status').then((res) => {
          if (res.success) {
            this.bpmStatusDictOptions = res.result;
          }
        });
      },
      startProcess(record){
        this.$confirm({
          title:'提示',
          content:'确认提交流程吗?',
          onOk:()=>{
            let params = {
              flowCode: this.flowCode,
              id: record.id,
              formUrl: 'dayin/modules/TableDayinWork',
              formUrlMobile: ''
            }
            postAction(this.url.startProcess, params).then(res=>{
              if(res.success){
                this.$message.success(res.message);
                this.loadData();
                this.onClearSelected();
              }else{
                this.$message.warning(res.message);
              }
            }).catch((e)=>{
              this.$message.warning('不识别的请求!');
            })
          }
        })
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'bpmStatus',text:'流程状态',dictCode:'bpm_status'})
        fieldList.push({type:'string',value:'taskId',text:'任务编号',dictCode:''})
        fieldList.push({type:'string',value:'taskName',text:'任务名称',dictCode:''})
        fieldList.push({type:'string',value:'taskNeirong',text:'任务内容',dictCode:''})
        fieldList.push({type:'string',value:'dingdanBianhao',text:'订单编号',dictCode:''})
        fieldList.push({type:'string',value:'dingdanKehu',text:'客户名称',dictCode:''})
        fieldList.push({type:'string',value:'dingdanShuliang',text:'模型件数',dictCode:''})
        fieldList.push({type:'string',value:'dingdanMoxingzhongliang',text:'模型重克',dictCode:''})
        fieldList.push({type:'string',value:'dingdanXiadanshijian',text:'下单时间',dictCode:''})
        fieldList.push({type:'string',value:'dingdanJiedanyuan',text:'接单员',dictCode:''})
        fieldList.push({type:'string',value:'dingdanYaoqiu',text:'订单要求',dictCode:''})
        fieldList.push({type:'string',value:'dingdanTupian',text:'模型图片',dictCode:''})
        fieldList.push({type:'string',value:'dingdanHuokuan',text:'货款状态',dictCode:''})
        fieldList.push({type:'string',value:'dingdanFahuofangshi',text:'发货方式',dictCode:''})
        fieldList.push({type:'string',value:'dingdanZhuangtai',text:'订单状态',dictCode:''})
        this.superFieldList = fieldList
      },
     handlePreviewPic: function(record){
        var flowCode = this.flowCode;
        var dataId = record.id;
        this.$refs.extActProcessInstPicModal.preview(flowCode,dataId);
        this.$refs.extActProcessInstPicModal.title="流程图";
      },
      //办理
      handleProcess(record){
        this.loading=true;
        console.log("我进来了",record);
        this.getProcessNodeInfo(record);
      },
      searchReset() {
        this.queryParam = {};
        this.model.userName ="";
        this.loadData(1);
      },
      selectUserOK: function(data){
        this.model.userName = data.realname;
        this.queryParam.userName = data.username;
      },
      handleSelect: function(){
        this.$refs.selectSingleUserModal.select();
      },
      getBizProcessNodeInfo(record){
        let url = "/act/process/extActProcessNode/getBizProcessNodeInfo"
        var params = {flowCode:this.flowCode,dataId:record.id};//查询条件
        getAction(url,params).then((res)=>{
          if(res.success){
            console.log("获取流程节点信息",res);
            console.log("表单数据",res.result.records)
            var  data = {
              dataId:res.result.dataId,
              taskId:res.result.taskId,
              flowCode:this.flowCode,
              taskDefKey:res.result.taskDefKey,
              procInsId:res.result.procInsId,
              tableName:res.result.tableName,
              permissionList:res.result.permissionList,
              bizTaskList:res.result.bizTaskList,
              vars:res.result.records
            }
            this.formData = data;
            this.path = res.result.formUrl;
            console.log("------获取流程节点信息>>",data);
            console.log("------流程表单地址>>",this.formUrl);
            this.$refs.taskDealModal.deal(data,this.formUrl);
            this.loading=false;
          }
        })
      },
      taskOk(){
        console.log("流程办理完成")
        this.loadData()
      },
      handleEntruster(data){
        var that = this;
        var params = {
          taskId:this.taskId,
          taskAssignee:data.username
        };//查询条件
        console.log("委托",params)
        putAction(that.url.taskEntrust, params).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
            that.loadData();
          }
        })

      },
      handleOk(){
        this.loadData();
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
<style lang="less" scoped>
.ant-card-body .table-operator{
  margin-bottom: 18px;
}
.anty-row-operator button{margin: 0 5px}
.ant-btn-danger{background-color: #ffffff}

.ant-modal-cust-warp{height: 100%}
.ant-modal-cust-warp .ant-modal-body{height:calc(100% - 110px) !important;overflow-y: auto}
.ant-modal-cust-warp .ant-modal-content{height:90% !important;overflow-y: hidden}
</style>